/*
 * @描述: 
 * @创建者: kilomChou
 * @Date: 2021-05-15 10:30:36
 * @最后编辑: kilomChou
 * @LastEditTime: 2021-05-15 15:58:14
 */

import { VSnake, VWorld } from "../../../data/XlsxVo";
import { RandUtil } from "../../../util/RandUtil";
import { Util } from "../../../util/Util";
import AIAwaySnake from "../../ai/AIAwaySnake";
import AIAwayWall from "../../ai/AIAwayWall";
import AIEat from "../../ai/AIEat";
import AITurn from "../../ai/AITurn";
import Snake from "../../snake/Snake";
import BaseWorldManager from "./BaseWorldManager";
const [util, ru] = [Util, RandUtil];

/** 世界AI蛇管理器 */
export default class WorldAISnakeMgr extends BaseWorldManager {

    start(): void {
        this.createAISnakes();
    }

    update(): void {

    }

    /** 创建指定数量的AI蛇到世界 */
    createAISnakes(count = VWorld.cfg.aiCount) {
        while (count--) {
            this.createAISnake().spawn();
        }
    }

    /** 创建AI蛇 */
    createAISnake(addToWorld = true) {
        //生成随机AI名
        let name: string;
        do name = VSnake.cfg.aiNamePre + util.zeroFill(ru.rand(0, 1000, true), 3);
        while (this.world.snakeMgr.snakeNameMap.has(name));
        //创建蛇并赋予AI
        let snake = this.world.snakeMgr.createSnake(name, false);
        snake.ais = [new AITurn({
            snake,
            checkTime: [1000, 2333],
            aiChance: 0.333,
            aiTime: [100, 1000],
            turnSpeed: [0.1, 1],
        }), new AIEat({
            snake,
            checkTime: 0,
            aiChance: 1,
            aiTime: 1000,
            findDistance: 3
        }), new AIAwayWall({
            snake,
            checkTime: 0,
            aiChance: 1,
            checkDistance: 10,
            aiTime: 1000,
        }), new AIAwaySnake({
            snake,
            checkTime: 0,
            aiChance: 1,
            aiTime: 1000,
            checkDistance: 10,
        })];
        addToWorld && this.addAISnake(snake);
        return snake;
    }

    /** 添加AI蛇到世界 */
    addAISnake(snake: Snake) {
        this.world.snakeMgr.addSnake(snake);
    }

}